use serde::{Deserialize, Serialize};
use crate::domain::dto::project_dto::ProjectUpdateRequest;

/// 项目表 api_project
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ProjectEntity {
    /** 主键 */
    pub id: Option<u64>,
    /** 项目名称 */
    pub project_name: String,
    /** 项目描述 */
    pub project_desc: Option<String>,
    /** 业务线ID */
    pub biz_id: u64,
    /** 全局参数 */
    pub global_param: Option<String>,
    /** 全局变量 */
    pub global_variable: Option<String>,
    /** 项目基础路径，支持环境的url */
    pub base_url: Option<String>,
}

impl ProjectEntity {
    pub fn update(mut self, target: ProjectUpdateRequest) -> Self {
        self.id = Some(target.id);
        self.project_name = target.name;
        self.project_desc = target.desc;
        self.biz_id = target.biz_id;
        self.base_url = if target.base_url.is_empty() { None } else { serde_json::to_string(&target.base_url).map_or(None, |s| None) };
        self
    }
}